package com.paas.form.queue.impl;

import com.paas.common.util.BeanUtil;
import com.paas.form.entity.FormApiLogEntity;
import com.paas.form.entity.FormApiLogParamEntity;
import com.paas.form.queue.FormApilogConsumer;
import com.paas.form.service.IFormApiLogParamService;
import com.paas.form.service.IFormApiLogService;
import com.paas.plus.tool.form.model.ApiLog;
import com.paas.plus.tool.form.server.FormService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service("formApilogConsumer")
public class FormApilogConsumerImpl implements FormApilogConsumer {

    @Autowired
    public IFormApiLogService formApiLogService;

    @Autowired
    public IFormApiLogParamService formApiLogParamService;

    @Override
    public void save(List<ApiLog> apiLogList) {
        if(BeanUtil.isNotEmpty(apiLogList)){

            for(ApiLog apiLog:apiLogList){
                FormApiLogEntity formApiLogEntity = new FormApiLogEntity();
                formApiLogEntity.setType(apiLog.getType());
                formApiLogEntity.setApiName(apiLog.getName());
                formApiLogEntity.setStartTime(apiLog.getStartDate());
                formApiLogEntity.setEndTime(apiLog.getEndDate());
                formApiLogEntity.setTotalTime(String.valueOf(apiLog.getTotalTime()));
                formApiLogEntity.setCallStatus(apiLog.getCallStatus());
                formApiLogEntity.setAuthAccount(apiLog.getThirdAppid());
                formApiLogEntity.setViewId(apiLog.getViewId());
                formApiLogEntity.setStatus("1");
                formApiLogEntity.setError(apiLog.getError());
                formApiLogService.save(formApiLogEntity); //添加访问日志主表
                FormApiLogParamEntity formApiLogParamEntity = new FormApiLogParamEntity();
                formApiLogParamEntity.setLogId(formApiLogEntity.getId());
                if(BeanUtil.isNotEmpty(apiLog.getJsonObject())){
                    formApiLogParamEntity.setReqParams(apiLog.getJsonObject().toJSONString());
                }
                formApiLogParamService.save(formApiLogParamEntity);
            }

        }
    }



}
